import prompt from '@system.prompt';
import router from '@system.router';
import dataStorage from '@ohos.data.storage'
import mediaLibrary from '@ohos.multimedia.mediaLibrary';
import featureAbility from '@ohos.ability.featureAbility';
import http from '@ohos.net.http';
import request from '@ohos.request';
export default {
    data: {
        body:'',
        title:'',
        tags:'',
        userid:'-1',
        context:null,
        path:null,
        cachepath:null,
        imageName:null,
        imagePath:null,
        hasPic:false,
    },
    TurnBack(){
        router.back();
    },
    async onShow(){
        this.context = featureAbility.getContext()
        this.path = await this.context.getFilesDir()
        this.cachepath = await this.context.getCacheDir()
    },
    ChooseCover(){
        let storage = dataStorage.getStorageSync(this.path + '/mystore')
        this.userid = storage.getSync('userid', '-1')
        if(this.userid === '-1'){
            prompt.showToast({
                message: '请先登录'
            })
            return;
        }

        var that = this
        let option = {type : "image", count : 1  };
        mediaLibrary.getMediaLibrary().startMediaSelect(option).then((value) => {
            console.log("Media resources selected."+value);             // Obtain the media selection value.
            // 使用calculate同步方法，返回OperateData对象。
            this.utilInterface = createLocalParticleAbility('edu.zut.hys.jsskateboard.util.UtilServiceAbility');
            this.utilInterface.CopyToCache(value[0],result => {
                console.info(this.lable+result)
                that.imageName = result;
                that.imagePath = "internal://cache/"+result;
                prompt.showToast({
                    message: '选择成功'
                })
                that.UploadImage();
            })
        }).catch((err) => {
            console.log("An error occurred when selecting media resources.");
            prompt.showToast({
                message: '选择失败'
            })
        });
    },
    UploadImage(){
        if(this.imageName!==null)
        request.upload({ url: 'http://101.35.82.76:9001/upload',
            files:[{
                       filename: this.imageName,
                       name:'file',
                       uri:'internal://cache/'+this.imageName,
                       type:'png',
                   }],
            data:[{name:"userid",value:this.userid}]
        },
            (err, data) => {
                if (err) {
                    prompt.showToast({
                        message: '上传失败'
                    })
                    console.error('Failed to request the upload. Cause: ' + JSON.stringify(err));
                    return;
                }
                prompt.showToast({
                    message: '上传成功'
                })
                this.hasPic = true;
//                router.back();
            });
    },
    UploadArticle(){
        var that = this
        var url;
        if(this.hasPic == true){
            url = "http://101.35.82.76:9002/article/createArticle?userid="+this.userid+
            '&title='+this.title+'&body='+this.body+'&tags='+this.tags+'&hasPic=true'
        }else{
            url = "http://101.35.82.76:9002/article/createArticle?userid="+this.userid+
            '&title='+this.title+'&body='+this.body+'&tags='+this.tags+'&hasPic=false'
        }
        let httpRequest= http.createHttp();
        httpRequest.request(url,
            {
                method: 'GET',
                readTimeout: 60000,
                connectTimeout: 60000
            },(err, data) => {
                if (!err) {
                    console.info('Result:' + data.result);
                    console.info('code:' + data.responseCode);
                    console.info('header:' + data.header);
                    prompt.showToast({
                        message: '发布成功'
                    })
                } else {
                    console.info('error:' + err.data);
                    prompt.showToast({
                        message: 'Send Error'+ JSON.stringify(err)
                    })
                }
            });
    },
    Title(param){
        this.title = param.text;
    },
    Body(param){
        this.body = param.text;
    },
    Tags(param){
        this.tags = param.text;
    },
}
